package com.net.framework.help.httprequest;

import java.io.File;
import java.net.FileNameMap;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Author：xiaobin.zhang
 * Data：2019/8/23
 * Desc：<图片上传参数拼装类>
 */
public class RequestMapParamBean {
    protected ConcurrentHashMap<String, Object> params;
    protected ConcurrentHashMap<String, File> fileParams;

    public ConcurrentHashMap<String, Object> getParams() {
        return params;
    }

    public void setParams(ConcurrentHashMap<String, Object> params) {
        this.params = params;
    }

    public ConcurrentHashMap<String, File> getFileParams() {
        return fileParams;
    }

    public void setFileParams(ConcurrentHashMap<String, File> fileParams) {
        this.fileParams = fileParams;
    }

    public void putParams(String key, Object value) {
        if (null == params) {
            params = new ConcurrentHashMap<>();
        }
        if (key != null && value != null) {
            params.put(key, value);
        }
    }

    public void putFileParams(String key, File file) {
        if (null == fileParams) {
            fileParams = new ConcurrentHashMap<>();
        }
        if (key != null && file != null) {
            fileParams.put(key, file);
        }
    }

    public String getMimeType(String fileUrl) {
        FileNameMap fileNameMap = URLConnection.getFileNameMap();
        String type = fileNameMap.getContentTypeFor(fileUrl);
        return type;
    }

    public Map<String, RequestBody> getMapRequestBody() {
        Map<String, RequestBody> mapRequestBody = new HashMap<>();
        if (null != getParams()) {
            for (ConcurrentHashMap.Entry<String, Object> entry : getParams().entrySet()) {
                mapRequestBody.put(entry.getKey(), toRequestBody(entry.getValue()));
            }
        }
        if (null != getFileParams()) {
            for (ConcurrentHashMap.Entry<String, File> entry : getFileParams().entrySet()) {
                File file = entry.getValue();
                String filename = entry.getKey();
                RequestBody requestFile = RequestBody.create(MediaType.parse(getMimeType(file.getAbsolutePath())), file);
                mapRequestBody.put("file\"; filename=\"" + filename, requestFile);
            }
        }
        return mapRequestBody;
    }

    public Map<String, RequestBody> getMultiMapRequestBody() {
        Map<String, RequestBody> mapRequestBody = new HashMap<>();
        if (null != getParams()) {
            for (ConcurrentHashMap.Entry<String, Object> entry : getParams().entrySet()) {
                mapRequestBody.put(entry.getKey(), toRequestBody(entry.getValue()));
            }
        }
        if (null != getFileParams()) {
            for (ConcurrentHashMap.Entry<String, File> entry : getFileParams().entrySet()) {
                File file = entry.getValue();
                String filename = entry.getKey();
                RequestBody requestFile = RequestBody.create(MediaType.parse(getMimeType(file.getAbsolutePath())), file);
                mapRequestBody.put("file[]\"; filename=\"" + filename, requestFile);
            }
        }
        return mapRequestBody;
    }

    private RequestBody toRequestBody(Object value) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain"), null == value ? "" : value.toString());
        return requestBody;
    }
}
